/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace ELE.EnterpriseLibrary.Data.Configuration
{
  /// <summary>
  /// Defines the settings of a <see cref="ObjectStorage"/> system that is a container of an 
  /// Object Relational Mapping for queries, objects, mappings and queryBuilders.
  /// <para>This class is used by the O/R Application Block to retrieve and store data for the configuration 
  /// of the application block.</para>
  /// <para>An example of the object mapping settings configuration file: </para>
  /// <code>
  /// &lt;?xml version="1.0" encoding="utf-8" ?>
  /// &lt;configuration>
  ///   &lt;configSections>
  ///     &lt;section name="objectStorage" type="ELE.EnterpriseLibrary.Data.Configuration.ObjectStorageSettings, ELE.EnterpriseLibrary" />
  ///   &lt;/configSections>
  ///   &lt;objectStorage defaultStorage="default">
  ///     &lt;storage>
  ///       &lt;add name="default" 
  ///            filePath="[Drive]:\Path\To\Mapping.config"/>
  ///     &lt;/storage>
  ///   &lt;/objectStorage>
  /// &lt;/configuration>
  /// </code>
  /// </summary>
  public class ObjectStorageSettings : SerializableConfigurationSection
  {
    private const string storageProperty = "storage";
    private const string defaultStorageProperty = "defaultStorage";
    /// <summary>
    /// The name of the data configuration section.
    /// </summary>
    public const string SectionName = "objectStorage";
    /// <summary>
    /// Default constructor used by factories.
    /// </summary>
    public ObjectStorageSettings()
      : base()
    {
    }
    /// <summary>
    /// Gets the object storage settings section from the configuration source
    /// </summary>
    /// <param name="configurationSource"></param>
    /// <returns></returns>
    public static ObjectStorageSettings GetObjectStorageSettings(IConfigurationSource configurationSource)
    {
      return (ObjectStorageSettings)configurationSource.GetSection(SectionName);
    }

    /// <summary>
    /// Gets the collection of <see cref="ObjectStorageData"/> configuration elements.
    /// </summary>
    [ConfigurationProperty(storageProperty)]
    public NamedElementCollection<ObjectStorageData> Storages
    {
      get
      {
        return (NamedElementCollection<ObjectStorageData>)base[storageProperty];
      }
    }
    /// <summary>
    /// The name of the default storage.
    /// </summary>
    [ConfigurationProperty(defaultStorageProperty, IsRequired = true)]
    public string DefaultStorage
    {
      get { return (string)base[defaultStorageProperty]; }
      set { base[defaultStorageProperty] = value; }
    }
  }
}
